package com.galleryviewpager.i_wave_loading;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class WaveLoadingView2 extends View {


    private Paint mWavePaint;
    private Paint mCirclePaint;

    // TODO: 2019/9/16 具体关系对应本包下面的paint_mode.png
    private PorterDuffXfermode mMode = new PorterDuffXfermode(PorterDuff.Mode.DST_OUT);

    private int mWidth, mHeight;

    private Canvas mCanvas;
    private Bitmap mBitmap;

    private Path mPath = new Path();

    public WaveLoadingView2(Context context) {
        this(context, null);
    }

    public WaveLoadingView2(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WaveLoadingView2(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    private void initPaint() {
        // TODO: 2019/9/17 关闭硬件加速 硬件加速对模式有影响
        setLayerType(View.LAYER_TYPE_SOFTWARE,null);
        mWavePaint = new Paint();
        mWavePaint.setAntiAlias(true);
        mWavePaint.setStrokeWidth(6f);
        mWavePaint.setColor(Color.RED);
//        mWavePaint.setXfermode(mMode);
//        mWavePaint.setStyle(Paint.Style.FILL);

        mCirclePaint = new Paint();
        mCirclePaint.setStyle(Paint.Style.FILL);
//        mCirclePaint.setXfermode(mMode);
        mCirclePaint.setColor(Color.parseColor("#99cc00"));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        if (widthMode == MeasureSpec.EXACTLY) {
            mWidth = widthSize;
        }

        if (heightMode == MeasureSpec.EXACTLY) {
            mHeight = heightSize;
        }
        setMeasuredDimension(mWidth, mHeight);
        mBitmap = Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
    }

    private int changeX = 0, changY = 300;

    @Override
    protected void onDraw(Canvas canvas) {

        if (changeX < 50) {
            changeX++;
        } else if (changeX > 90) {
            changeX--;
        }
        if (changY > 100) {
            changY--;
        }

//        canvas.drawCircle(mWidth / 2, mHeight / 2, 150, mCirclePaint);
        mWavePaint.setColor(Color.RED);
        mWavePaint.setXfermode(mMode);
        canvas.drawRect(0, 0, mBitmap.getWidth(), mBitmap.getHeight(), mWavePaint);
        mWavePaint.setXfermode(null);

        mPath.reset();
        mPath.moveTo(0, changY);
        mPath.cubicTo(changeX, 100 + changY, changeX + 150, changY - 100, mBitmap.getWidth(), changY);
        mPath.lineTo(mBitmap.getWidth(), mBitmap.getHeight());
        mPath.lineTo(0, mBitmap.getHeight());
        mPath.close();
        mWavePaint.setColor(Color.YELLOW);
        canvas.drawPath(mPath, mWavePaint);

        postInvalidateDelayed(10);

    }
}
